package com.ds.common.web.jackson;

import cn.hutool.core.date.DatePattern;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.TimeZone;

/**
 * jackson 配置
 *
 * @author fs
 */
@Slf4j
@Configuration
@AutoConfigureBefore(JacksonAutoConfiguration.class)
public class JacksonConfig {

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer customizer() {
        return builder -> {
            // 序列化时字段值为 null 时不输出
            builder.serializationInclusion(JsonInclude.Include.NON_NULL);
            builder.timeZone(TimeZone.getDefault());
            builder.simpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
            // 反序列化时未知枚举值设为null
            builder.featuresToEnable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);
            // 忽略未知字段
            builder.featuresToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
            builder.modules(new JavaTimeModule());
            log.info("初始化 jackson 配置");
        };
    }


    public static class JavaTimeModule extends SimpleModule {
        public JavaTimeModule() {
            this.addSerializer(LocalDateTime.class
                    , new LocalDateTimeSerializer(DatePattern.NORM_DATETIME_FORMATTER));
            this.addSerializer(LocalDate.class
                    , new LocalDateSerializer(DatePattern.NORM_DATE_FORMATTER));
            this.addSerializer(LocalTime.class
                    , new LocalTimeSerializer(DatePattern.NORM_TIME_FORMATTER));
            this.addDeserializer(LocalDateTime.class
                    , new LocalDateTimeDeserializer(DatePattern.NORM_DATETIME_FORMATTER));
            this.addDeserializer(LocalDate.class
                    , new LocalDateDeserializer(DatePattern.NORM_TIME_FORMATTER));
            this.addDeserializer(LocalTime.class
                    , new LocalTimeDeserializer(DatePattern.NORM_TIME_FORMATTER));
            this.addSerializer(Long.class, ToStringSerializer.instance);
            this.addSerializer(Long.TYPE, ToStringSerializer.instance);
            this.addSerializer(BigInteger.class, ToStringSerializer.instance);
            this.addSerializer(BigDecimal.class, ToStringSerializer.instance);
        }
    }
}
